﻿@model System.String
@using Telerik.Web.Mvc.UI

@{
    Html.Telerik().EditorFor(model => model)
        //.Name("editor_" + ViewData.TemplateInfo.GetFullHtmlFieldName(string.Empty))
        .FileBrowser(browser => browser.Browse("Browse", "ImageBrowser")
                              .Thumbnail("Thumbnail", "ImageBrowser")
                              .Upload("Upload", "ImageBrowser")
                              .DeleteFile("DeleteFile", "ImageBrowser")
                              .DeleteDirectory("DeleteDirectory", "ImageBrowser")
                              .CreateDirectory("CreateDirectory", "ImageBrowser"))
        .Tools(tools => tools
            .Separator()
            .Custom(settings => settings.HtmlAttributes(new
            {
                @class = "t-table",
                onclick =
                    "insertTable()"
            }))
            .Separator()
            .Custom(settings => settings.HtmlAttributes(new { @class = "t-html t-html_" + (ViewData.TemplateInfo.GetFullHtmlFieldName(string.Empty)) }))
        )
        .Localizable(SF.GetLangCode())
        .HtmlAttributes(new { @class = "editor_" + (ViewData.TemplateInfo.GetFullHtmlFieldName(string.Empty)) })
        .Encode(false)
        .Render();
}

<script type="text/javascript">

function insertTable() {
    var editor = $('.editor_@(ViewData.TemplateInfo.GetFullHtmlFieldName(string.Empty))').data("tEditor");
    editor.exec('insertHtml', {value: '<table class="t-editor-table" border="0" cellpadding="0" cellspacing="0" style="width:100%;"><tr><td>&nbsp;</td><td>&nbsp;</td></tr><tr><td>&nbsp;</td><td>&nbsp;</td></tr></table>'});
    editor.update();
    editor.focus();
}

var htmlSourcePopup_@(ViewData.TemplateInfo.GetFullHtmlFieldName(string.Empty));
function showHtml_@(ViewData.TemplateInfo.GetFullHtmlFieldName(string.Empty))(e) {
    e.stopPropagation();
    e.preventDefault();

    var editor_@(ViewData.TemplateInfo.GetFullHtmlFieldName(string.Empty)) = $('.editor_@(ViewData.TemplateInfo.GetFullHtmlFieldName(string.Empty))').data('tEditor');
    var html_@(ViewData.TemplateInfo.GetFullHtmlFieldName(string.Empty)) = editor_@(ViewData.TemplateInfo.GetFullHtmlFieldName(string.Empty)).value();
    if (!htmlSourcePopup_@(ViewData.TemplateInfo.GetFullHtmlFieldName(string.Empty))) {
        htmlSourcePopup_@(ViewData.TemplateInfo.GetFullHtmlFieldName(string.Empty)) =
            $('<div class="html-view">' +
                    '<div class="textarea t-state-default"><textarea></textarea></div>' +
                    '<div class="t-button-wrapper">' + 
                        '<button id="htmlUpdate_@(ViewData.TemplateInfo.GetFullHtmlFieldName(string.Empty))" class="t-button t-state-default">@Html.R(VirtualPath,"Update")</button>' +
                        '<button id="htmlCancel_@(ViewData.TemplateInfo.GetFullHtmlFieldName(string.Empty))" class="t-button t-state-default">@Html.R(VirtualPath,"Cancel")</button>' +
                    '</div>' +
                '</div>')
            .css('display', 'none')
            .tWindow({
                title: '@Html.R(VirtualPath,"HTML")',
                modal: true, 
                resizable: false, 
                draggable: true,
                width: 700,
                onLoad: function() {
                    var $popup = $(this);
                    $popup.find('.textarea')
                            .css('width', function() {
                                return 700 - (this.offsetWidth - $(this).width());
                            })
                            .focus()
                            .end()
                            .find('#htmlCancel_@(ViewData.TemplateInfo.GetFullHtmlFieldName(string.Empty))')
                            .click(function() {
                                htmlSourcePopup_@(ViewData.TemplateInfo.GetFullHtmlFieldName(string.Empty)).close();
                            })
                            .end()
                            .find('#htmlUpdate_@(ViewData.TemplateInfo.GetFullHtmlFieldName(string.Empty))')
                            .click(function() {
                                var value = $popup.find('textarea').val();
                                editor_@(ViewData.TemplateInfo.GetFullHtmlFieldName(string.Empty)).value(value);
                                htmlSourcePopup_@(ViewData.TemplateInfo.GetFullHtmlFieldName(string.Empty)).close();
                            });
                },
                onClose: function() {
                    editor_@(ViewData.TemplateInfo.GetFullHtmlFieldName(string.Empty)).focus();
                },
                effects: $.telerik.fx.toggle.defaults()
        })
        .data('tWindow');
    }
    $(htmlSourcePopup_@(ViewData.TemplateInfo.GetFullHtmlFieldName(string.Empty)).element).find('textarea').text(html_@(ViewData.TemplateInfo.GetFullHtmlFieldName(string.Empty)));
    htmlSourcePopup_@(ViewData.TemplateInfo.GetFullHtmlFieldName(string.Empty)).center().open();
}
</script>
@{ Html.Telerik().ScriptRegistrar().OnDocumentReady(@<text> 
    $('.t-html_@(ViewData.TemplateInfo.GetFullHtmlFieldName(string.Empty))').click(showHtml_@(ViewData.TemplateInfo.GetFullHtmlFieldName(string.Empty)));

    $("#Window_@(ViewData.TemplateInfo.GetFullHtmlFieldName(string.Empty)) .t-link").live("click", function () {
        var href = $(this).attr("href");
        if(href.indexOf("http://") != -1) href = href.replace(/^.*\/\/[^\/]+/, '');
        linkButtonAdd(href);
        $("#Window_@(ViewData.TemplateInfo.GetFullHtmlFieldName(string.Empty))").data("tWindow").close();
        return false;
    });
</text>); }